package com.touch.jgloo.core;
import java.awt.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
import com.touch.graphics.UVType;
import com.touch.graphics.Point3D;
/**
 * A JGLQuad represents a 2D object composed of 4 points. Those
 * points are described in 3D space and can be set to 
 * non-co-plain points.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public class JGLQuad extends JGLObject
{		
	/**
	 * Builds a default Quad using 32x32 size.
	 * @since r461
	 */	
	public JGLQuad()
	{
		//create a 4-point polygon to use
		addPolygon(new JGLPolygon(4));
		//build the object as a quad
		setQuad(JGLConstants.TILERECT);
	}
	/**
	 * Constructs the Quad using W & H values representing Width and Height respectively 
	 * @param w Width of the Quad
	 * @param h Height of the Quad
	 * @since r461
	 */
	public JGLQuad(final int w, final int h)
	{		
		//define the points
		final int verts[] = {0, 0, 0, w, 0, 0, w, h, 0,	0, h, 0};
		
		//add a new polygon
		this.addPolygon(new JGLPolygon(4));
		//build the quad
		this.setQuad(verts);
	}
	/**
	 * Returns the color of the Quad
	 * @return color Shader used to color the Quad
	 * @since r461
	 */
	public Color getColor()
	{
		final JGLPolygon polygon = this.getPolygon(0);
		//return the color of the default polygon
		//this assumes that each polygon has the same color
		return polygon.getColor(0);
	}
	/**
	 * Resizes the JGLQuad
	 * @param w New width of the quad
	 * @param h New height of the quad
	 * @since r461
	 */
	public final synchronized void resize(final int w, final int h)
	{
		//create the new verts
		final int verts[] = {0, 0, 0, w, 0, 0, w, h, 0, 0, h, 0};
		//set the new quad
		this.setQuad(verts);
	}		
	/**
	 * Sets the Quad's shape from an array of floats
	 * @param verts An array of floats containing the (x,y,z) coordinates of each point.
	 * @since r461
	 */
	public final synchronized void setQuad(final int ...verts)
	{
		//get the quad as a polygon
		final JGLPolygon polygon = this.getPolygon(0);		
		//set the render state of the polygon to QUAD
		//QUAD tells OpenGL to draw a 4-point polygon
		polygon.renderState = GL.GL_QUADS;
		//build the 1st point off of vertices 0, 1, and 2
		polygon.setPoint(0, new Point3D(verts[0], verts[1], verts[2]));
		//build the 2nd point off of vertices 3, 4, and 5
		polygon.setPoint(1, new Point3D(verts[3], verts[4], verts[5]));
		//build the 3rd point off of vertices 6, 7, and 8
		polygon.setPoint(2, new Point3D(verts[6], verts[7], verts[8]));
		//build the 4th point off of vertices 9, 10, and 11
		polygon.setPoint(3, new Point3D(verts[9], verts[10], verts[11]));
	}
	/**
	 * Sets the color of the Quad with a QuadShader
	 * @param color QuadShader used to color the quad
	 * @since r461
	 */
	public void setColor(final Color ...color)
	{
		//get the Quad as a polygon
		final JGLPolygon polygon = this.getPolygon(0);
		//set the 1st point to the top-left color of the QuadShader
		polygon.setColor(0, color[0]);
		//set the 2nd point to the top-right color of the QuadShader
		polygon.setColor(1, color[1]);
		//set the 3rd point to the bottom-right color of the QuadShader
		polygon.setColor(2, color[2]);
		//set the 4th point to the bottom-left color of the QuadShader
		polygon.setColor(3, color[3]);
	}
	/**
	 * Sets the color of the Quad with a single color
	 * @param color Shader used to color the Quad.
	 * @since r461
	 */
	public void setColor(final Color color)
	{
		//get the quad as a polygon
		final JGLPolygon polygon = this.getPolygon(0);
		//scroll through each of the 4 points and set the color values
		for(int i =0; i < 4; i++)
		{
			//sets the color of the polygon to a specific color
			polygon.setColor(i, color);
		}
	}
	/**
	 * Sets the color of a point
	 * @param index Point to set the color to. Must be [0, 3]
	 * @param color Color to set that point to.
	 * @since r461
	 */
	public void setColor(final int index, final Color color)
	{
		//get the quad as a polygon
		final JGLPolygon polygon = this.getPolygon(0);
		//scroll through each of the 4 points and set the color values
		polygon.setColor(index, color);
	}	
	/**
	 * Returns teh color of the Quad assuming that there are 4 different colors
	 * @param index Point to get color from
	 * @return Color of the point
	 * @since r461
	 */
	public Color getColor(final int index)
	{		
		//get the polygon's pointer
		final JGLPolygon polygon = this.getPolygon(0);
		//return the polygon's color
		return polygon.getColor(index);
	}
	/** 
	 * Returns the polygon representing the Quad
	 * @return A 4-point polygon of a Quad
	 * @since r461
	 */
	public final JGLPolygon getPolygon()
	{
		//return the quad as a polygon
		return this.getPolygon(0);
	}
	/**
	 * Sets the texture of the Quad
	 * @param texture Texture to set the Quad
	 * @since r461
	 */
	public void setTexture(final Texture texture)
	{
		//get the polygon used for the Quad
		final JGLPolygon polygon = this.getPolygon();
		//overwrite the texture pointers
		polygon.setTexture(texture);
		this.setUV(UVType.QUADMAP);		
	}
	/**
	 * Sets the UV of the object. Only supports QUADMAP at the moment.
	 * @param UV_STYLE
	 */
	public final void setUV(final UVType uv)
	{
		if(uv == UVType.QUADMAP)
		{			
			setUV(0, 0, 0, 0);
			setUV(0, 1, 1, 0);
			setUV(0, 2, 1, 1);
			setUV(0, 3, 0, 1);	
		}		
	}	
	/**
	 * Overrides the "String toString" method of Object
	 * @return String representing the JGLQuad
	 * @since r461
	 */
	public String toString()
	{
		final JGLPolygon polygon = this.getPolygon();
		return "JGLQuad: " + 
			polygon.getPoint(0).toString() + ", " +
			polygon.getPoint(1).toString() + ", " +
			polygon.getPoint(2).toString() + ", " + 
			polygon.getPoint(3).toString();		
	}
	public JGLQuad clone()
	{
		return (JGLQuad)super.clone();
	}
}

